package com.example.demo.config;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.JsonInclude;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import io.lettuce.core.RedisClient;
//import io.lettuce.core.RedisURI;
//import io.lettuce.core.TrackingArgs;
//import io.lettuce.core.api.StatefulRedisConnection;
//import io.lettuce.core.support.caching.CacheAccessor;
//import io.lettuce.core.support.caching.CacheFrontend;
//import io.lettuce.core.support.caching.ClientSideCaching;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
//import java.time.Duration;
//import java.time.temporal.ChronoUnit;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: demo
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2022-04-27 09:42
 **/
//@Configuration
public class RedisConfig {

//    public static Map<String, String> clientCache = new ConcurrentHashMap<>();

//    @Bean
//    public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory factory){
//        RedisTemplate<String,Object> template = new RedisTemplate <>();
//        template.setConnectionFactory(factory);
//
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(om);
//
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        // key采用String的序列化方式
//        template.setKeySerializer(stringRedisSerializer);
//        // hash的key也采用String的序列化方式
//        template.setHashKeySerializer(stringRedisSerializer);
//        // value序列化方式采用jackson
//        template.setValueSerializer(jackson2JsonRedisSerializer);
//        // hash的value序列化方式采用jackson
//        template.setHashValueSerializer(jackson2JsonRedisSerializer);
//        template.afterPropertiesSet();
//        return template;
//    }

//    @Bean
//    public StatefulRedisConnection statefulRedisConnection(){
//        RedisURI redisUri = RedisURI.builder()
//                .withHost("192.168.56.105")
//                .withPort(6379)
//                // 注意这里只能是0号库
//                .withDatabase(0)
//                .withTimeout(Duration.of(10, ChronoUnit.SECONDS))
//                .build();
//        RedisClient redisClient = RedisClient.create(redisUri);
//        return redisClient.connect();
//    }
//
//    @Bean
//    public CacheFrontend<String, String> cacheFrontend(StatefulRedisConnection statefulRedisConnection){
//        return ClientSideCaching.enable(CacheAccessor.forMap(clientCache),
//                statefulRedisConnection,
//                TrackingArgs.Builder.enabled());
//    }

}
